#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

DIR=$PWD
WORK_ROOT=$PWD

function prepare_env() {
    export JAVA_HOME=$DIR/flutter_env/jdk-17.0.8

    export PUB_HOSTED_URL=https://pub.flutter-io.cn
    export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

    export FLUTTER_HOME=$DIR/flutter_flutter
    export PATH=$JAVA_HOME/bin:$FLUTTER_HOME/bin:$PATH

    export TOOL_HOME=$DIR/command-line-tools
    export DEVECO_SDK_HOME=$TOOL_HOME/sdk
    export NODE_HOME=$DIR/flutter_env/node-v16.14.2-linux-x64
    export PATH=$TOOL_HOME/ohpm/bin:$TOOL_HOME/hvigor/bin:$NODE_HOME/bin:$PATH
}

function download_diff() {
    local pr_num=$1
	local flutter_name=$2
    local token="h9M9m8Y4APoeqcqgLsh2RnUz"
    local url="https://api.gitcode.com/api/v5/repos/openharmony-sig/$flutter_name/pulls/$pr_num/diff"

    curl --location \
  $url \
  --header 'PRIVATE-TOKEN: h9M9m8Y4APoeqcqgLsh2RnUz' \
  -o $pr_num.diff

    if [ $? -ne 0 ]
    then
        echo "curl failed!"
        return 1
    fi
    return 0
}

# function get_project_name() {
#     # 默认构建项目路径为空
#     DEFALUT_PROJECT=''
#     local diff_url=$1
# 	  local pr_num=${diff_url##*/}
# 	  local flutter_name=$(echo "$diff_url" | sed -n 's#.*/\([^/]*\)/\(pull\|merge_requests\)/.*#\1#p')
#     local diff_name=${diff_url##*/}.diff
#     local retval=1
#     if [ ! -f $diff_name ]
#     then
#         download_diff $pr_num $flutter_name
#         if [ $? -ne 0 ]
#         then
# 			echo "error: get project_name failed, download diff file failed"
#             echo "$DEFALUT_PROJECT"
#             return $retval
#         fi
#     fi
#     local diff_str=$(grep "diff --git" "$diff_name")
#     # 匹配a/开头，/ohos/entry/.* 中间的路径，因为不同项目的目录结构不同
#     local regex1="a/(.*)/ohos/entry/.*"
#     # 匹配a/开头，*_ohos/ohos/.* 中间的路径，因为不同项目的目录结构不同
#     local regex2="a/(.*)/*_ohos/.*"
#     for diff in $diff_str
#     do
#     local tmp=$(echo "$diff" | grep "/")
#     if [ ! -z "$tmp" ]
#     local find=0
#     then
#         if [[ $tmp =~ $regex1 ]]; then
#             tmp=${BASH_REMATCH[1]}
#             echo "$tmp"
#             retval=0
#             find=1
#             break
#         fi

#         if [[ $tmp =~ $regex2 ]]; then
#             tmp=${BASH_REMATCH[1]}_ohos/example

#             if [ ! -d "$tmp" ]; then
#               tmp=${BASH_REMATCH[1]}
#             fi

#             echo "$tmp"
#             retval=0
#             find=1
#             break
#         fi
#     fi
#     done
#     if [[ $find -eq 0 ]]
#     then
# 	    echo "error: get project_name failed. cannot find the project path"
#         echo "$DEFALUT_PROJECT"
#     fi
#     return $retval
# }
PROJECT='example'

function start_compile() {
    local flutter_name=$(echo "$@" | sed -n 's#.*/\([^/]*\)/\(pull\|merge_requests\)/.*#\1#p')

    #配置环境变量
    prepare_env

    #执行构建
    echo "项目位置：$PROJECT"
    echo "PATH============== $PATH"
    echo "HOS_SDK_HOME============== $HOS_SDK_HOME"
    echo "NODE: $(node --version), $(which node)"
    ls -alh $TOOL_HOME/tool/node/bin/node
    echo "OHPM: $(ohpm --version) $(which ohpm)"
    ls -alh $TOOL_HOME/ohpm/bin/ohpm

    flutter doctor -v
    cd ./third_party/flutter/$flutter_name/$PROJECT && flutter pub get && rm -rf ./pubspec.lock
    flutter build hap --target-platform ohos-arm64 --debug
    echo $DIR
    mv ./ohos/entry/build/default/outputs/default/entry-default-unsigned.hap $DIR/hap_out
}

# shellcheck disable=SC2068
start_compile $@

exit $?

#eof
